<!--
 * @Descripttion:
 * @version:
 * @Author: YouHuJun
 * @Date: 2021-10-21 10:50:05
 * @LastEditors: youhujun 2900976495@qq.com
 * @LastEditTime: 2025-05-08 21:38:22
-->
<template>
  <div>
    <el-table
      ref="voiceConfigTable"
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :data="AttributeConfigList"
      border
      stripe
      highlight-current-row
      empty-text
      fit
      height="70vh"
      :row-key="getRowKeys"
      style="width: 100%"
      :expand-row-keys="expandIds"
      @selection-change="handleSelectionChange"
      @expand-change="handleExpandChange"
    >
      <el-table-column
        type="selection"
        align="center"
        width="55"
      />
      <el-table-column
        label="序号"
        type="index"
        width="55"
        align="center"
        fixed
      />
      <el-table-column
        label="详情"
        type="expand"
        align="center"
        fixed
      >
        <template slot-scope="props">
          <tableInfo :row-data="props.row" :show.sync="show" />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="attribute_name"
        label="属性名称"
        min-width="200"
      />
      <el-table-column
        align="center"
        prop="attribute_code"
        label="属性代码"
        min-width="200"
      />
      <el-table-column
        align="center"
        prop="note"
        label="属性备注"
        min-width="200"
      />
      <el-table-column
        align="center"
        prop="sort"
        label="排序"
        min-width="100"
      />
      <el-table-column
        align="center"
        prop="created_at"
        label="创建时间"
        min-width="200"
      />
      <el-table-column label="操作" align="center" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="handleEdit(scope.$index, scope.row)"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            el-button
            @click="handleDelete(scope.$index, scope.row)"
          >删除
            <!-- 删除(禁用)结束 -->
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作2" align="center" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="handleToShowValue(scope.$index, scope.row)"
          >查看属性值
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { deleteGoodsAttributeName, multipleDeleteGoodsAttributeName } from '@/api/youhu-shop/V1/goods/attribute'
import tableInfo from './tableInfo'
export default
{
  // 组件名称
  name: 'VoiceConfigTable',
  // 组件
  components:
    {
      tableInfo
    },
  props:
    {
      loading: {
        type: Boolean,
        default: true
      },
      // eslint-disable-next-line vue/prop-name-casing
      AttributeConfigList: {
        type: Array,
        default: () =>
        {
          return []
        }
      },
      dialogVisible: {
        type: Boolean,
        default: false
      },
      dialogTitle: {
        type: String,
        default: ''
      },
      dialogType: {
        type: Number,
        default: 1
      },
      beforeAttributeConfigForm: {
        type: Object,
        default: null
      },
      reverse: {
        type: Number,
        default: 0
      },
      multipleDelete: {
        type: Number,
        default: 0
      }
    },
  // 数据
  data()
  {
    return {
      // 控制详情显示
      show: 1,
      baseUrl: '',
      // 选中队象的数组
      multipleSelection: [],
      // 选中元素的下标
      selectIndex: [],
      // 反选元素的下标
      diffIndex: [],
      // 选中的下标
      selectId: [],
      // 存放展开的id, 每次只展开一行
      getRowKeys(row)
      {
        return row.id
      },
      expandIds: []
    }
  },
  // 计算属性
  computed:
    {
    },
  // 监听
  watch:
    {
      reverse(value)
      {
        this.diffIndex = []

        const all = []

        for (let index = 0; index < this.AttributeConfigList.length; index++)
        {
          all.push(index)
        }

        this.diffIndex = all.filter(val =>
        {
          return !this.selectIndex.includes(val)
        })

        // 实现反选的效果
        this.$refs.voiceConfigTable.clearSelection()
        this.diffIndex.forEach(index =>
        {
          this.$refs.voiceConfigTable.toggleRowSelection(this.AttributeConfigList[index])
        })

        this.selectIndex = this.diffIndex
      },
      // 批量删除
      multipleDelete(value)
      {
        this.selectId = []
        this.getSelectId()
        const selectId = {}
        selectId['selectId'] = this.selectId
        selectId['is_delete'] = 14
        const that = this
        this.$confirm('确认要批量删除系统配置项吗？', '确认信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '删除',
          cancelButtonText: '取消'
        })
          .then(() =>
          {
            multipleDeleteGoodsAttributeName(selectId).then((res) =>
            {
              if (res && res.code === 0)
              {
                that.$emit('hanldeUpdateData', true)
                that.$message(
                  {
                    message: res.msg,
                    type: 'success'
                  })
              }
            })
          })
          .catch(action =>
          {
            return
          })
      }
    },
  // 实例创建之前
  beforeCreate()
  {
  },
  // 创建
  created()
  {
  },
  // 挂载之前
  beforeMount()
  {
  },
  // 挂载 --常用
  mounted()
  {
    this.baseUrl = process.env.VUE_APP_URL
  },
  // 更新之前
  beforeUpdate()
  {
  },
  // 跟新后
  updated()
  {
  },
  // 销毁之前
  beforeDestroy()
  {
  },
  // 销毁后
  destroyed()
  {
  },
  // 方法
  methods:
  {

    /**
         * 过滤管理员状态
         */
    filterSystemConfigItemType(value, row)
    {
      return row.item_type === value
    },

    // 修改管理员
    handleEdit(index, row)
    {
      this.$emit('update:dialogVisible', true)
      this.$emit('update:dialogTitle', '修改配置项')
      this.$emit('update:dialogType', 2)
      this.$emit('update:beforeAttributeConfigForm', row)
    },

    handleDelete(index, row)
    {
      const id = {}
      id['id'] = row.id
      id['is_delete'] = 4
      this.deleteGoodsAttributeName(id, index)
    },

    // 删除
    deleteGoodsAttributeName(id, index)
    {
      const that = this
      this.$confirm('确认要删除该地址吗？', '确认信息', {
        distinguishCancelAndClose: true,
        confirmButtonText: '删除',
        cancelButtonText: '取消'
      })
        .then(() =>
        {
          deleteGoodsAttributeName(id).then((res) =>
          {
            if (res && res.code === 0)
            {
              that.$emit('hanldeUpdateData', true)
              that.$message(
                {
                  message: res.msg,
                  type: 'success'
                })
            }
          })
        })
        .catch(action =>
        {
          return
        })
    },
    // 点击查看属性值
    handleToShowValue(index, row)
    {
      // console.log(row)
      this.$router.push({ 'name': 'showGoodsAttributeValue', 'query': { attribute_name_id: row.id }})
    },
    // 多选
    handleSelectionChange(val)
    {
      this.multipleSelection = val

      this.selectIndex = []
      this.multipleSelection.forEach(item =>
      {
        this.selectIndex.push(this.AttributeConfigList.indexOf(item))
      })
    },
    // 获取选中下标
    getSelectId()
    {
      this.selectIndex.forEach(item =>
      {
        this.selectId.push(this.AttributeConfigList[item].id)
      })
    },
    /**
     * 展开
     * @param {*} row
     * @param {*} expandedRows
     */
    handleExpandChange(row, expandedRows)
    {
      this.show = 1
      if (expandedRows.length)
      {
        this.expandIds = []
        if (row)
        {
          this.expandIds.push(row.id)
        }
      }
      else
      {
        this.expandIds = []
      }
    }
  }

}
</script>
<style lang='scss' scoped>
</style>
